<?php

namespace app\api\controller;

use app\api\model\Word;
use app\common\controller\Api;
use PhpOffice\PhpWord\Shared\ZipArchive;

/**
 * 错题库接口
 */
class Cuoti extends Api
{
    protected $noNeedLogin = ['print'];
    protected $noNeedRight = ['*'];

    //标签
    public function label()
    {
        $cuoti_model = new \app\api\model\Cuoti();

        $data = [
            'subject' => $cuoti_model->getSubjectList(),
            'source' => $cuoti_model->getSourceList(),
            'proficiency'=>$cuoti_model->getProficiencyList()
        ];
        $this->success('请求成功',$data);
    }

    /**
     * 添加错题
     *
     */
    public function add()
    {
        $subject = input('subject','yuwen');
        $image = input('image','');
        //$title = input('title','');
        //$answer = input('answer','');
        $answer_img = input('answer_img','');
        $remark = input('remark','');
        $source_label = input('source_label',0);
        $proficiency_label = input('proficiency_label',0);

        if (empty($source_label))$this->error('请选择错题出处');
        if (empty($proficiency_label))$this->error('请选择掌握程度');

        $cuoti_model = new \app\api\model\Cuoti();

        $data = [
            'user_id'=>$this->auth->id,
            'subject'=>$subject,
            'image'=>$image,
            'title'=>$_POST['title']??'',
            'answer'=>$_POST['answer']??'',
            'answer_img'=>$answer_img,
            'remark'=>$remark,
            'source_label'=>$source_label,
            'proficiency_label'=>$proficiency_label,
            'category_id'=>$this->auth->category_id,
        ];

        $res = $cuoti_model->save($data);
        if ($res){
            $count = \app\api\model\Cuoti::where(['user_id'=>$this->auth->id])->count();
            /*$level_1 = \app\common\model\User::nextlevel($count);
            $level_2 = \app\common\model\User::nextlowlevel($count);*/
            $currentLevels = \app\common\model\User::calculateLevels($count);
            $level_1 = $currentLevels['largeLevel'];
            $level_2 = $currentLevels['smallLevel'];
            $user = $this->auth->getUser();
            $user->level = $level_1;
            $user->low_level = $level_2;
            $user->save();

            $this->success('添加成功');
        } else {
            $this->error('添加失败');
        }

    }

    //修改
    public function edit()
    {
        $id = input('id',0);
        if (empty($id))$this->error('缺少参数id');
        $subject = input('subject','yuwen');
        $image = input('image','');
        //$title = input('title','');
        //$answer = input('answer','');
        $answer_img = input('answer_img','');
        $remark = input('remark','');
        $source_label = input('source_label',0);
        $proficiency_label = input('proficiency_label',0);

        $cuoti_model = new \app\api\model\Cuoti();

        $data = [
            'user_id'=>$this->auth->id,
            'subject'=>$subject,
            'image'=>$image,
            'title'=>$_POST['title']??'',
            'answer'=>$_POST['answer']??'',
            'answer_img'=>$answer_img,
            'remark'=>$remark,
            'source_label'=>$source_label,
            'proficiency_label'=>$proficiency_label,
        ];
        $res = $cuoti_model->save($data,['id'=>$id]);

        $this->success('编辑成功');
    }

    //详情
    public function info()
    {
        $id = input('id',0);
        if (empty($id))$this->error('缺少参数id');

        $detail = \app\api\model\Cuoti::get($id);

        $this->success('成功',$detail);
    }


    //错题列表
    public function lists()
    {
        $subject = input('subject','');
        $keyword = input('keyword','');
        $source_label = input('source_label',0);
        $proficiency_label = input('proficiency_label','');
        $time = input('time','all');

        $where = ['subject'=>$subject,'user_id'=>$this->auth->id];
        if (empty($subject))$this->error('缺少参数科目');

        if ($keyword){
            $where['title'] = ['like', '%' . $keyword . '%'];
        }
        if ($source_label){
            $where['source_label'] = $source_label;
        }
        if ($proficiency_label){
            $where['proficiency_label'] = $proficiency_label;
        }
        // 获取当前时间戳
        $currentTimestamp = time();

        // 获取三天前的时间戳
        $threeDaysAgoTimestamp = strtotime('-3 days', $currentTimestamp);

        // 获取七天前的时间戳
        $sevenDaysAgoTimestamp = strtotime('-7 days', $currentTimestamp);

        // 获取三十天前的时间戳
        $thirtyDaysAgoTimestamp = strtotime('-30 days', $currentTimestamp);
        if ($time==1){
            //3天内
            $where['createtime'] = ['>', $threeDaysAgoTimestamp];
        } elseif ($time==2){
            //7天内
            $where['createtime'] = ['>', $sevenDaysAgoTimestamp];
        } elseif ($time==3){
            //30天内
            $where['createtime'] = ['>', $thirtyDaysAgoTimestamp];
        } elseif($time==4) {
            //自定义
            $start_time = input('start_time','');
            $end_time = input('end_time','');

            $where['createtime'] = ['between', [strtotime($start_time), strtotime($end_time)]];
        }

        $orderBy = 'createtime desc';

        $list = \app\api\model\Cuoti::where($where)
            ->order($orderBy)
            ->paginate(10, false, ['query' => request()->get()]);

        $this->success('请求成功',$list);
    }


    //批量打标签
    public function batchLabel()
    {
        $ids = input('ids','');
        $subject = input('subject','yuwen');
        $source_label = input('source_label',0);
        $proficiency_label = input('proficiency_label',0);
        $category_id = input('category_id',0);
        $other_label = input('other_label','');

        $data = ['subject'=>$subject,'source_label'=>$source_label,'proficiency_label'=>$proficiency_label,'category_id'=>$category_id];
        if ($other_label){
            $data['other_label'] = $other_label;
        }
        $res = \app\api\model\Cuoti::where('id','in',$ids)->update($data);

        $this->success('');
    }

    //批量删除
    public function batchDel()
    {
        $ids = input('ids','');

        $res = \app\api\model\Cuoti::where('id','in',$ids)->delete();

        $this->success('成功');
    }




    //打印
    public function print()
    {

        $subject = input('subject','yuwen');
        $topic = input('topic',0);
        $answer = input('answer',0);
        $ids = input('ids','');
        $type = input('type',0);
        $title = input('title','');
        $model = new Word();

        if ($type){
            $res = $model->printTable($ids,$topic,$answer);
        } else {
            $res = $model->print($ids,$subject,$topic,$answer);
        }

        $this->success('',$res);
    }
}
